<?php

require_once('../inc/init.php');
require_once  '../inc/socket.class.php';
//权限判断
//chkpurview('act_notice');

//服务器列表
$sql = "SELECT s.id AS server_id,s.server,s.platform_id,p.`name` as platform_name,s.innerip1
		FROM tb_server AS s
		LEFT JOIN tb_platform AS p ON p.id = s.platform_id";
$list = $db->getAll($sql);

$platform_list = array();
$server_list = array();
$platform_array = array();

foreach($list as $key=>$val)
{
	$platform_list[$val['platform_id']] = $val['platform_name'];	//所有平台列表
	$platform_array[$key] = $val['platform_id'];
}

$platform_id = isset($_REQUEST['platform_id'])?$_REQUEST['platform_id']:$platform_array[0];
$server_id = isset($_GET['server_id'])?$_GET['server_id']:0;

foreach($list as $k1=>$v1)
{
	if($v1['platform_id'] != $platform_id)
	{
		continue;
	}
	$server_list[$v1['server_id']] = $v1['server'];
	$backendurl_list[$v1['server_id']] = $v1['innerip1'];
}


$action = isset($_GET['action']) ? $_GET['action'] : null;
if($action == 'send')
{
	$timing = isset($_GET['timing']) ? strtotime($_GET['timing']) : NULL;
	
	$content = isset($_GET['content']) ? $_GET['content'] : NULL;
	$content = stripslashes($content);
	$content = str_replace("\r",'',$content);
	$content = str_replace("\n",'',$content);
	$content = str_replace("\t",'',$content);
	$content = str_replace("<strong>",'<b>',$content);
	$content = str_replace("</strong>",'</b>',$content);
	$content = str_replace("&nbsp;",'',$content);
		
	$content = str_replace('<span style="color:', '<font color="', $content);
	$content = str_replace('</span>', '</font>', $content);
	
	$post_fields['time'] = $timing;
	$post_fields['content'] = $content;
	
	$send_info = '';
	$msg = '';
	$i=0;
	foreach($server_id as $k=>$v)
	{
		$send_url = 'http://' . $backendurl_list[$v] . '/backend/task/act_notice.php';
		
		$retcode = makeRequest($send_url, $post_fields);
		
		if((!empty($retcode)) && ($retcode == 00000000))				//成功
		{
			$msg[$i]['server'] = $server_list[$v];
			$msg[$i]['status'] = 1;
		}
		else			//管理员添加失败
		{
			$msg[$i]['server'] = $server_list[$v];
			$msg[$i]['status'] = 0;
			$msg[$i]['serverId'] = $v;
		}
		$i++;
	}
	echo json_encode($msg); 
	exit;	
	
}

$T->assign('platform_id',$platform_id);
$T->assign('server_id',$server_id);
$T->assign('platform_list',$platform_list);
$T->assign('server_list',$server_list);

$T->display('act_notice.html');


